package com.zhiyou100.video.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhiyou100.video.model.Video;
import com.zhiyou100.video.service.CourseService;
import com.zhiyou100.video.service.SpeakerService;
import com.zhiyou100.video.service.VideoService;

@Controller
@RequestMapping("/admin")
public class VideoController {
	
	@Autowired
	private VideoService vs;
	
	@Autowired
	private CourseService cs;
	
	@Autowired
	private SpeakerService ss;
	
	@RequestMapping("/video")
	public String list(String video_title,String course,String speaker,Model model) {
		Map<String, Object> map = new HashMap<>();
		map.put("video_title", video_title);
		map.put("course", course);
		map.put("speaker", speaker);
		List<Video> list = vs.selectVideo(map);
		model.addAttribute("videos", list);
		model.addAttribute("courses", cs.selectAll());
		model.addAttribute("speakers",ss.selectAll());
		return "backstage/video/video";
	}
	
	@RequestMapping("/addvideo")
	public String add(Model model) {
		model.addAttribute("courses", cs.selectAll());
		model.addAttribute("speakers",ss.selectAll());
		return "backstage/video/edit_video";
	}
	
	@RequestMapping("/video/insert")
	public String insertvideo(Video video) {
		Date date = new Date();
		video.setInsert_time(date);
		int i = vs.insertVideo(video);
		if(i>0) {
			return "redirect:../video";
		}
		return "backstage/video/edit_video";
	}
	
	
	@RequestMapping("/upvideo")
	public String update(int id,Model model) {
		Video video = vs.selectVideoById(id);
		model.addAttribute("video", video);
		model.addAttribute("courses", cs.selectAll());
		model.addAttribute("speakers",ss.selectAll());
		return "backstage/video/edit_video";
	}
	
	@RequestMapping("/video/update")
	public String updatevideo(Video video) {
		Date date = new Date();
		video.setUpdate_time(date);
		int i = vs.updateVideo(video);
		if(i>0) {
			return "redirect:../video";
		}
		return "backstage/video/edit_video";
	}
	
	
	@RequestMapping("/video/delete")
	public String deleteById(String[] check) {
		int i = vs.deleteById(check);
		if(i>0) {
			return "redirect:../video";
		}
		return null;
	}
	
}
